#include "tabfun.h"
#include "debug.h"

#define DEBUG 0

// Functions on the function datatype
char* pp_function(char *buf, Fonction* f) {
	if (f->class != NULL)
		sprintf(buf, "%s::%s", f->nom, f->class->nom);
	else
		sprintf(buf, "%s", f->nom);
}

void insp_function(Fonction* f) {
	debug_print("Fonction { nom: %s, args.size: %d, ret_type: %s, class: %s}\n",
				f->nom,
				ll_size(f->args),
				pp_type(f->ret_type),
				f->class ? f->class->nom : "<none>"
	);
}

// Functions on the class datatype
char* pp_class(Class* cl) {
	return cl->nom;
}
Fonction* cl_get_method(Class* cl, char* id) {
	assert(cl != NULL && cl->tf != NULL);
	debug_print("looking up method '%s' in class '%s'\n", id, cl->nom);

	Fonction *f = NULL;
	while (cl != NULL && f == NULL) {
		f = tf_get(cl->tf, id);
		cl = cl->parent;
	}
	return (Fonction*)f;
}

// Functions on the function table
tabfun* tf_create() {
	return (tabfun*)ht_create();
}

Fonction* tf_get(tabfun* tf, char const* id) {
    return (Fonction*)ht_get((ht_table*)tf, id);
}

void tf_add(tabfun* tf, char const* id, Fonction* f) {
    ht_set((ht_table*)tf, id, f);
}

// Functions on the class table
tabfun* tc_create() {
	return (tabfun*)ht_create();
}

Class* tc_get(tabfun* tc, char const* id) {
    return (Class*)ht_get((ht_table*)tc, id);
}
void tc_add(tabfun* tc, char const* id, Class* f) {
    ht_set((ht_table*)tc, id, f);
}
